package com.cloud.pole.user.controller;

import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.user.model.vo.account.UserAccountDetailVo;
import com.cloud.pole.user.service.account.UserAccountDetailService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("userAccountDetail/front")
@Slf4j
public class UserAccountDetailController{

	@Autowired
	UserAccountDetailService userAccountDetailService;
	
	@ResponseBody
    @RequestMapping(value = "/getUserAccountDetailList")
    public ApiRestResponse<List<UserAccountDetailVo>> getUserAccountDetailList(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String accountId,
    		@RequestParam(defaultValue = "") String pageSize,
    		@RequestParam(defaultValue = "") String pageNum
    		){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
			ParamException.err(StringUtils.isBlank(accountId), "accountId不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(accountId), "accountId错误");
			
			ParamException.err(StringUtils.isBlank(pageSize), "pageSize不能为空");
			ParamException.err(!RegexUtils.isInteger(pageSize), "pageSize错误");
			
			ParamException.err(StringUtils.isBlank(pageNum), "pageNum不能为空");
			ParamException.err(!RegexUtils.isInteger(pageNum), "pageNum错误");
			PageInfo<UserAccountDetailVo> pageInfo = userAccountDetailService.getAccountDetailList(token, Long.parseLong(accountId), Integer.parseInt(pageNum), Integer.parseInt(pageSize));
			return ApiRestResponse.successOfPage(CommonConstants.STATUS_OK_MESSAGE, (int)pageInfo.getPages(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getList());
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
}
